package com.nmf.site.biz.compcollege.comp.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.nmf.dao.gen.bean.BaseCollege;
import com.nmf.dao.gen.bean.BizCompany;
import com.nmf.site.basedata.category.enums.CategoryType;
import com.nmf.site.basedata.category.service.BaseBizCategoryService;
import com.nmf.site.basedata.category.service.BaseCategoryService;
import com.nmf.site.basedata.college.service.BaseCollegeService;
import com.nmf.site.biz.comp.service.BizCompanyService;
import com.nmf.site.biz.compcollege.comp.bean.CompCollegeBean;
import com.nmf.site.biz.compcollege.comp.enums.CompTypeEnums;
import com.nmf.site.biz.compcollege.comp.service.CompCollegeService;
import com.nmf.site.common.excep.SiteException;
import com.nmf.site.common.persistence.Page;
@Controller
@RequestMapping(value = "${adminPath}/compCollege/comp")
public class BizCompSchoolController {
	@Autowired
	BaseCategoryService baseCategoryService;
	@Autowired
	private BizCompanyService bizCompanyService;
	@Autowired
	BaseBizCategoryService baseBizCategoryService;
	@Autowired
	CompCollegeService compCollegeService;
	@Autowired
	BaseCollegeService baseCollegeService;
	
	@RequiresPermissions("comp:bizCompany:view")
	@RequestMapping(value = "")
	public String index() {
		return "compCollege/comp/compIndex";
	}
	
	
	@RequestMapping(value = "compCateList")
	public String compTree(Model model) {
		Map<String,List<BizCompany>> dmap = compCollegeService.queryTreeCompByCategory();
		model.addAttribute("dmap", dmap);
		model.addAttribute("categoryList", baseBizCategoryService.queryCategory(CategoryType.company));
		return "compCollege/comp/compTree";
	}
	
	@RequestMapping(value = {"search"})
	public String list(BaseCollege baseCollege,String companyId, HttpServletRequest request, HttpServletResponse response, Model model) {
		try{
			Page<CompCollegeBean> page = compCollegeService.findPage(new Page<BaseCollege>(request, response,10000),  baseCollege,companyId,CompTypeEnums.compCollege); 
			
			model.addAttribute("page", page);
			model.addAttribute("companyId", companyId);
			model.addAttribute("baseCollege", baseCollege);
		}catch(SiteException e){
			model.addAttribute("message", e.getMessage());
			return "compCollege/comp/collegeList";
		}
		return "compCollege/comp/collegeList";
	}
	
	@RequestMapping(value = {"save"})
	public String list(@RequestParam("compChecked") String[] compChecked, @RequestParam("compIds") String[] compIds,HttpServletRequest request, HttpServletResponse response, Model model) {
		try{
			compCollegeService.saveCompCollege(compChecked,compIds,CompTypeEnums.compCollege);
			BaseCollege baseCollege = new BaseCollege();
			Page<CompCollegeBean> page = compCollegeService.findPage(new Page<BaseCollege>(request, response),  baseCollege,compIds[0],CompTypeEnums.compCollege); 
			model.addAttribute("page", page);
			model.addAttribute("baseCollege", new BaseCollege());
		}catch(SiteException e){
			model.addAttribute("message", e.getMessage());
			return "compCollege/comp/collegeList";
		}
		return "compCollege/comp/collegeList";
	}

	@RequestMapping(value = "none")
	public String none() {
		return "compCollege/comp/compNone";
	}
}
